package app.controllers.mobileweb;

import app.Const;
import app.dtos.MoviePlayDto;
import app.interceptors.MobileWxMemberInterceptor;
import app.models.movie.Movie;
import app.models.movie.Record;
import com.alibaba.fastjson.JSON;
import com.github.sog.controller.BasicController;
import com.github.sog.kit.Tuple;
import com.github.sog.kit.date.DateProvider;
import com.google.common.base.Strings;
import com.jfinal.aop.Before;

import java.util.List;

/**
 * <p>
 * .影视
 * </p>
 *
 * @author liuhui
 * @version 1.0 14-5-13 下午9:31
 */
@Before(MobileWxMemberInterceptor.class)
public class MovieController extends BasicController {

    public void index() {
        List<Movie> nowMovies = Movie.dao.findByStates(Const.MOVIE_STATUS.STATUS_1);
        List<Movie> commingMovies = Movie.dao.findByStates(Const.MOVIE_STATUS.STATUS_2);
        setAttr("nowMovies", nowMovies);
        setAttr("commingMovies", commingMovies);
        render("index.ftl");
    }


    public void showCnt() {
        String movid = getPara("movid");
        if (!Strings.isNullOrEmpty(movid)) {
            Movie movie = Movie.dao.findById(movid);
            if(null != movie){
                String play_info = movie.getStr("play_time");
                List<MoviePlayDto> moviePlayDtos = JSON.parseArray(play_info, MoviePlayDto.class);
                Tuple tuple = new Tuple(movie, moviePlayDtos);
                setAttr("tuple", tuple);
                setAttr("movie", movie);
                render("movieCnt.ftl");
            }
        }
    }

    public void booking() {
        String mid = getPara("mid");
        int movid = getParaToInt("movid");
        Movie.dao.updateBookingNo(movid);
        Record record = new Record();
        boolean save = record.set("movie", movid)
                .set("member", mid)
                .set("create_time", DateProvider.DEFAULT.getCurrentDate())
                .save();
        if (save) {
            renderSuccess();
        } else {
            renderError("预定记录失败");
        }
    }
}
